﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using OracleDataToRedis.Utils;
using OracleDataToRedis.Domain;

namespace OracleDataToRedis.Coders.Services
{
    public class BaseServiceCoder
    {
        public static void Write()
        {
            string path = BaseParams.ServicesPath;
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(path, "BaseService.cs");

            #region 创建domain cs文件
            FileStream file = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(file, Encoding.UTF8);
            CommentsCoder.CreateCsComments("业务服务基类", sw);
            sw.WriteLine("using System;");
            sw.WriteLine("using System.Collections;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.Data;");
            sw.WriteLine("using System.Text;");
            sw.WriteLine("using " + BaseParams.PersistenceNameSpace + ";");
            sw.WriteLine("using " + BaseParams.UtilityNameSpace + ";");
            sw.WriteLine("using " + BaseParams.DomainNameSpace + ";");
            sw.WriteLine("");
            sw.WriteLine("namespace " + BaseParams.ServicesNameSpace + "");
            sw.WriteLine("{");
            sw.WriteLine("    public class BaseService<T> where T : BaseEntity");
            sw.WriteLine("    {");
            sw.WriteLine("        protected String hql_slf = \"\";");
            sw.WriteLine("        protected String hql_pnt = \"\";");
            //sw.WriteLine("        protected String hql_son = \"\";");
            //sw.WriteLine("        protected String hql_pnt_son = \"\";");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 实体数据操作对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        public EntityDao<T> EntityDao");
            sw.WriteLine("        {");
            sw.WriteLine("            get;");
            sw.WriteLine("            set;");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        public BaseService()");
            sw.WriteLine("        {");
            sw.WriteLine("            this.EntityDao = new EntityDao<T>();");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        //保存或者更新对象信息");
            sw.WriteLine("        public void SaveOrUpdate(T t)");
            sw.WriteLine("        {");
            sw.WriteLine("            this.EntityDao.SaveOrUpdate(t);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        // 保存或者更新对象信息");
            sw.WriteLine("        public void SaveOrUpdateList(IList<T> lst)");
            sw.WriteLine("        {");
            sw.WriteLine("            this.EntityDao.SaveOrUpdate(lst);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        // 保存客户信息");
            sw.WriteLine("        public object Save(T t)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.EntityDao.Save(t);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        // 更新对象信息");
            sw.WriteLine("        public void Update(T t, object key)");
            sw.WriteLine("        {");
            sw.WriteLine("            this.EntityDao.Update(t, key);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        // 删除对象信息");
            sw.WriteLine("        public void Delete(T t)");
            sw.WriteLine("        {");
            sw.WriteLine("            this.EntityDao.Delete(t);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        // 删除对象信息");
            sw.WriteLine("        public void DeleteList(IList<T> lst)");
            sw.WriteLine("        {");
            sw.WriteLine("            this.EntityDao.Delete(lst);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        // 加载对象信息");
            sw.WriteLine("        public T Get(Object id)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.EntityDao.Get(id);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过hql条件获取对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hql\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public T UniqueResult(string condi, QueryType qt, params object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            string hql = this.GetCurrentHql(qt) + condi;");
            sw.WriteLine("            return (T)this.EntityDao.UniqueResult(hql, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过Hql自由组合条件获取对象信息");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"condi\"></param>");
            sw.WriteLine("        /// <param name=\"qt\"></param>");
            sw.WriteLine("        /// <param name=\"paging\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList<T> GetEntitysByHqlCondi(String condi, QueryType qt,");
            sw.WriteLine("                Paging paging, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            long fristResult = 0, maxResult = 0;");
            sw.WriteLine("            if (paging != null)");
            sw.WriteLine("            {");
            sw.WriteLine("                fristResult = paging.StartRowIndex;");
            sw.WriteLine("                maxResult = paging.PageSize;");
            sw.WriteLine("                if (paging.IsCount == true)");
            sw.WriteLine("                {");
            sw.WriteLine("                    string hql = this.GetCurrentHql(qt);");
            sw.WriteLine("                    hql = hql.Substring(hql.IndexOf(\"from\"));");
            sw.WriteLine("                    String hql_count = \"select count(*) \"");
            sw.WriteLine("                            + hql.Replace(\"fetch \", \"\") + condi.Substring(0, condi.IndexOf(\"order by\"));");
            sw.WriteLine("                    Object o = this.EntityDao.UniqueObjectResult(hql_count, ps);");
            sw.WriteLine("                    long lng = (long)o;");
            sw.WriteLine("                    paging.RowCount = lng;");
            sw.WriteLine("                }");
            sw.WriteLine("            }");
            sw.WriteLine("            string hql1 = this.GetCurrentHql(qt) + condi;");
            sw.WriteLine("            return this.EntityDao.Query(hql1, fristResult, maxResult, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过提交完整Hql语句，进行查询");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hql\"></param>");
            sw.WriteLine("        /// <param name=\"paging\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList<T> HqlQuery(String hql, Paging paging, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            long fristResult = 0, maxResult = 0;");
            sw.WriteLine("            if (paging != null)");
            sw.WriteLine("            {");
            sw.WriteLine("                fristResult = paging.StartRowIndex;");
            sw.WriteLine("                maxResult = paging.PageSize;");
            sw.WriteLine("                if (paging.IsCount == true)");
            sw.WriteLine("                {");
            sw.WriteLine("                    if (hql.IndexOf(\"group\") == -1)");
            sw.WriteLine("                    {");
            sw.WriteLine("                        String chql = hql.Substring(hql.IndexOf(\"from\"));");
            sw.WriteLine("                        String hql_count = \"select count(*) \"");
            sw.WriteLine("                            + chql.Replace(\"fetch \", \"\").Substring(0, chql.IndexOf(\"order by\"));");
            sw.WriteLine("                        Object o = this.EntityDao.UniqueObjectResult(hql_count, ps);");
            sw.WriteLine("                        long lng = (long)o;");
            sw.WriteLine("                        paging.RowCount = lng;");
            sw.WriteLine("                    }");
            sw.WriteLine("                    else");
            sw.WriteLine("                    {");
            sw.WriteLine("                        String chql = hql.Substring(hql.IndexOf(\"from\"));");
            sw.WriteLine("                        String sql_count = \"select count(*) from (select count(*) \"");
            sw.WriteLine("                                + chql.Replace(\"fetch \", \"\").Substring(0, hql.IndexOf(\"order by\")) + \")\";");
            sw.WriteLine("                        Object o = this.EntityDao.SqlQuery(sql_count, ps)[0];");
            sw.WriteLine("                        long lng = (long)o;");
            sw.WriteLine("                        paging.RowCount = lng;");
            sw.WriteLine("                    }");
            sw.WriteLine("                }");
            sw.WriteLine("            }");
            sw.WriteLine("            return this.EntityDao.Query(hql, fristResult, maxResult, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过提交完整Sql语句，进行查询");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"paging\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList<U> SqlQuery<U>(String sql, Paging paging, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            long fristResult = 0, maxResult = 0;");
            sw.WriteLine("            if (paging != null)");
            sw.WriteLine("            {");
            sw.WriteLine("                fristResult = paging.StartRowIndex;");
            sw.WriteLine("                maxResult = paging.PageSize;");
            sw.WriteLine("                if (paging.IsCount == true)");
            sw.WriteLine("                {");
            sw.WriteLine("                    if (sql.IndexOf(\"group\") == -1)");
            sw.WriteLine("                    {");
            sw.WriteLine("                        String csql = sql.Substring(sql.IndexOf(\"from\"));");
            sw.WriteLine("                        String sql_count = \"select count(*) \"");
            sw.WriteLine("                                + csql.Substring(0, csql.IndexOf(\"order by\"));");
            sw.WriteLine("                        Object o = this.EntityDao.SqlQuery(sql_count, ps)[0];");
            sw.WriteLine("                        long lng = (long)o;");
            sw.WriteLine("                        paging.RowCount = lng;");
            sw.WriteLine("                    }");
            sw.WriteLine("                    else");
            sw.WriteLine("                    {");
            sw.WriteLine("                        String csql = sql.Substring(sql.IndexOf(\"from\"));");
            sw.WriteLine("                        String sql_count = \"select count(*) from (select count(*) \"");
            sw.WriteLine("                                + csql.Substring(0, csql.IndexOf(\"order by\")) + \")\";");
            sw.WriteLine("                        Object o = this.EntityDao.SqlQuery(sql_count, ps)[0];");
            sw.WriteLine("                        long lng = (long)o;");
            sw.WriteLine("                        paging.RowCount = lng;");
            sw.WriteLine("                    }");
            sw.WriteLine("                }");
            sw.WriteLine("            }");
            sw.WriteLine("            return this.EntityDao.SqlQuery<U>(sql, fristResult, maxResult, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过执行Sql语句，返回DataTable");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public DataTable SqlFillDataTable(String sql, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.EntityDao.SqlFillDataTable(sql, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 获取对应对象的所有列表信息");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hsqtype\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList<T> GetAllEntitys(QueryType hsqtype)");
            sw.WriteLine("        {");
            sw.WriteLine("            String hql = this.GetCurrentHql(hsqtype);");
            sw.WriteLine("            return this.EntityDao.Query(hql, 0, 0);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        private string GetCurrentHql(QueryType hsqtype)");
            sw.WriteLine("        {");
            sw.WriteLine("            switch (hsqtype)");
            sw.WriteLine("            {");
            sw.WriteLine("                case QueryType.hql_slf:");
            sw.WriteLine("                    return this.hql_slf;");
            sw.WriteLine("                case QueryType.hql_pnt:");
            sw.WriteLine("                    return this.hql_pnt;");
            //sw.WriteLine("                case QueryType.hql_son:");
            //sw.WriteLine("                    return this.hql_son;");
            //sw.WriteLine("                case QueryType.hql_pnt_son:");
            //sw.WriteLine("                    return this.hql_pnt_son;");
            sw.WriteLine("            }");
            sw.WriteLine("            return this.hql_slf;");
            sw.WriteLine("        }");
            sw.WriteLine("    }");
            sw.WriteLine("}");
            sw.Close();
            file.Close();
            #endregion
        }
    }
}
